package com.imonsoft.sqm.web.basedata;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.imonsoft.common.constant.CCommon;
import com.imonsoft.common.constant.ConstantSqm;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.sqm.pojo.basedata.Dictionary;
import com.imonsoft.sqm.service.basedata.DictionaryService;
import com.imonsoft.sqm.validator.basedata.DictionaryValidator;

@Controller
@RequestMapping("/basedata/dictionary")
public class DictionaryController {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private DictionaryValidator dictionaryValidator;


	@RequestMapping("/listUI.do")
	public void listUI() {
	}

    @RequestMapping(value = "/ajaxList.do", method = RequestMethod.POST)
    public void ajaxList(HttpServletResponse response, Pager pager, Dictionary dictionary) {
		DataGrid<Dictionary> dataGrid = new DataGrid<Dictionary>();
		try {
			dataGrid = dictionaryService.getDataGrid(dictionary, pager);
		} catch (SystemException se) {
            logger.info(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
	}
    @RequestMapping(value = "/ajaxListType.do", method = RequestMethod.POST)
    public void ajaxListType(HttpServletResponse response, Pager pager, Dictionary dictionary) {
        DataGrid<Dictionary> dataGrid = new DataGrid<Dictionary>();
        try {
            dataGrid = dictionaryService.getDataGridDictionaryType(pager);
        } catch (SystemException se) {
            logger.info(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
    }

	@RequestMapping("/addUI.do")
	public void addUI(ModelMap model) {
	}

	@RequestMapping(value = "/ajaxAdd.do", method = RequestMethod.POST)
	public void ajaxAdd(HttpServletResponse response, Dictionary dictionary, BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		try {
			dictionaryValidator.validate(dictionary, result);
			if (result.hasErrors()) {
				message.setObj(dictionaryValidator.getErrors(result));
			} else {
				dictionaryService.insert(dictionary);
				message.setSuccess(true);
                message.setMsg(ConstantSqm.OPERATE_SUCCESS);
			}
		} catch (SystemException se) {
            logger.info(se.getMessage());
            message.setMsg(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
            message.setMsg(CCommon.EXCEPTION_MSG);
        }
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

	@RequestMapping(value = "/modifyUI.do")
	public void modifyUI(ModelMap model,Long id) {
        if(null!=id){
            Dictionary dictionary = dictionaryService.get(id);
            model.put("dictionary", dictionary);
        } else {
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
	}

	@RequestMapping(value = "/ajaxModify.do", method = RequestMethod.POST)
	public void ajaxModify(HttpServletResponse response, Dictionary dictionary, BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		try {
            dictionaryValidator.validate(dictionary, result);
            if (result.hasErrors()) {
                message.setObj(dictionaryValidator.getErrors(result));
            } else {
                dictionaryService.update(dictionary);
                message.setSuccess(true);
                message.setMsg(ConstantSqm.OPERATE_SUCCESS);
            }

		} catch (SystemException se) {
            logger.info(se.getMessage());
            message.setMsg(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
            message.setMsg(CCommon.EXCEPTION_MSG);
        }
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

    @RequestMapping(value = "/ajaxDelete.do", method = RequestMethod.POST)
    public void ajaxDelete(HttpServletResponse response,String[] ids) {
		ReturnMessage message = new ReturnMessage();
		try {
			List<Long> idList = new ArrayList<Long>();
//			String[] idStr = request.getParameter("ids").split("[,]");
			for (String id : ids) {
				idList.add(Long.valueOf(id));
			}
			dictionaryService.deleteDictionary(idList);
            message.setSuccess(true);
            message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
            logger.info(se.getMessage());
            message.setMsg(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
            message.setMsg(CCommon.EXCEPTION_MSG);
        }
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
}
